package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UrlUtility;
import model.PriorityDTO;
import bizlogic.PriorityManager;;

@WebServlet("/DeletePriority")
public class DeletePriorityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public DeletePriorityServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}
	
	private void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		PriorityManager pm = new PriorityManager();
		PriorityDTO pDTO;
		
		int pCode;
		boolean valid = true;
		String error = "";
		
		try {
			pCode = Integer.parseInt(request.getParameter("code"));
			
			pDTO = pm.getPriority(pCode);
			error = pm.deletePriority(pDTO);
			
			if ( !error.trim().equalsIgnoreCase("") ) valid = false;
			
		} catch ( NumberFormatException e) {
			valid = false;
			error = "priority.error.invalidnumbercode";
		}

		UrlUtility.redirectPage(response, "LoadPriority");		
	}
}
